#!/bin/bash

# TZAI Flutter Web 部署脚本
# 部署到阿里云服务器 39.97.249.188

set -e

SERVER="39.97.249.188"
SERVER_USER="root"
REMOTE_PATH="/usr/share/nginx/html/tzApp"
LOCAL_BUILD_PATH="fapp/build/web"

echo "======================================"
echo "TZAI Flutter Web 部署脚本"
echo "======================================"
echo ""
echo "服务器: $SERVER"
echo "远程路径: $REMOTE_PATH"
echo ""

# 检查是否已构建
if [ ! -d "$LOCAL_BUILD_PATH" ]; then
    echo "❌ 错误: 未找到构建产物"
    echo "请先运行: cd fapp && ./build-web.sh"
    exit 1
fi

echo "1. 检查构建产物..."
echo "✅ 构建产物存在"
echo ""

# 创建远程目录
echo "2. 创建远程目录..."
ssh $SERVER_USER@$SERVER "mkdir -p $REMOTE_PATH"

# 上传文件
echo "3. 上传文件到服务器..."
rsync -avz --delete \
    --exclude='.DS_Store' \
    $LOCAL_BUILD_PATH/ \
    $SERVER_USER@$SERVER:$REMOTE_PATH/

echo ""
echo "======================================"
echo "✅ 部署完成！"
echo "======================================"
echo ""
echo "🌐 访问地址: http://$SERVER/tzApp/"
echo ""
echo "📝 提示："
echo "1. 确保 Nginx 已配置 /tzApp 路径"
echo "2. 如需配置 Nginx，请参考 fapp/build-web.sh 中的示例"
echo ""

